package dreamCookGame.conveyorBeltMiniGame;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;

import javax.swing.Timer;

import dreamCookGame.Scene;
import dreamCookGame.Item;
import dreamCookGame.References;

public class ConveyorBeltMiniGame extends Scene {

	Bag _bag;
	Bag _bagFront;
	Box[] _boxes;
	int _counter;
	int _boxCounter;
	ConveyorBelt _conveyorBelt;
	boolean _firstTime = true;
	Timer _timer;
	public boolean _gameDone;
	
	
	public ConveyorBeltMiniGame(References reference) {
		super("conveyorBeltGame", "conveyorBeltBackground", null, 0, 0, null, null, null);
		
		_conveyorBelt = new ConveyorBelt(reference);
		_counter = 0;
		
		String[] bagBackImages = {"bagPlainBack"};
		_bag = new Bag(bagBackImages, 400, 380);
		String[] bagFrontImages = {"bagPlainFront"};
		_bagFront = new Bag(bagFrontImages, 401, 402);
		
		_boxes = new Box[2];
		String[] box1Images = {"box1"};
		_boxes[0] = new Box(box1Images, 100, -100, "3tsp");
		_boxes[1] = new Box(box1Images, 700, -100, "8floz");
		//_boxes[2] = new Box(box1Images, 600, 100, "16cups");
		//_boxes[3] = new Box(box1Images, 300, 100, "16oz");		

		_itemsBack = new Item[3];
		_itemsBack[0] = _bag;
		_itemsBack[1] = _boxes[0];
		_itemsBack[2] = _boxes[1];
		//_itemsBack[3] = _boxes[2];
		//_itemsBack[4] = _boxes[3];
		
		_itemsFront = new Item[1];
		_itemsFront[0] = _bagFront;
		
		_boxCounter = 0;
		
		int _MILLESECONDS_BETWEEN_BOXES = 4000;
		_timer = new Timer(_MILLESECONDS_BETWEEN_BOXES, new ActionListener() {
			public void actionPerformed(ActionEvent event) {
				_timer.stop();
				step();
				_timer.restart();
			}

			private void step() {
				if(!_firstTime)
					releaseBox();
			}
		});
		_timer.start();
	}

	public void moveBag(char direction) {
		switch(direction) {
		case 'r':
			_bag.moveRight();
			_bagFront.moveRight();
			break;
		case 'l':
			_bag.moveLeft();
			_bagFront.moveLeft();
			break;
		}
	}
	
	public void stopBag(char direction) {
		switch(direction) {
		case 'r':
			_bag.stopRight();
			_bagFront.stopRight();
			break;
		case 'l':
			_bag.stopLeft();
			_bagFront.stopLeft();
			break;
		}
	}
	
	public void checkItemsPosition() {
		for(int i = 0; i < _boxes.length; i++) {			
			if(_boxes[i] != null) {
				
				int state = _bag.getItemState(_boxes[i]._bottomCorners);
	
				System.out.println(state);
				
				switch(state) {
				case 0: //item falling (do nothing)
						break;
				case 1: //item hit bag side (make item disappear)
						break;
				case 2: //item in bag (add to bag)
						addBoxToBag(_boxes[i]);
						break;
				case 3: //item hit floor (make item disappear)
						_boxes[i].stop();
						break;
				}
			}
		}
	}
	
	public void addBoxToBag(Box box) {
		//_bag.loadBox();
		// box.stop();
		_gameDone = true;
	}
	
	public void update() {
		if(_firstTime) {
			boxesGetInfo();
			_firstTime = false;
		}
		
		checkItemsPosition();

		_conveyorBelt.setToNextFrame();
		this._backgroundImg = _conveyorBelt._currentFrame;
		
		for(int i = 0; i < _boxes.length; i++) {
				if(_boxes[i]._active)
						_boxes[i].shift();
				//_boxes[i].scaleUp(2.0);
		}

		_bag.shift();
		_bagFront.shift();
	}
	
	public void releaseBox() {
		if(_boxCounter >= _boxes.length)
			_boxCounter = 0;
		
		if(!_boxes[_boxCounter]._active)
		{
			Point[] startLocations = {new Point(100, 400), new Point(200, 400), new Point(300, 400), new Point(500, 400)};
			Point position = startLocations[(int) (Math.random() * startLocations.length)];
			_boxes[_boxCounter].start();

			_boxCounter++;
		}
		
	}
	
	public void boxesGetInfo() {
		for(int i = 0; i < _boxes.length; i++) {
			_boxes[i].getBottomCorners();
		}
	}
	
}
